<script lang="ts" setup>
import { getPatientConsultOrderListAPI } from '@/apis/consult'
import { DeletePatientOrderAPI } from '@/apis/order'
import {
  OrderStatus,
  type GetPatientConsultOrderListData,
  type GetPatientConsultOrderListParams,
  type OrderItem,
} from '@/types/consult'
import { ref } from 'vue'

const props = defineProps<{
  type: number
}>()

const query = ref<Required<GetPatientConsultOrderListParams>>({
  type: props.type,
  current: 1,
  pageSize: 5,
})
//loading状态
const loadMoreStatus = ref<UniHelper.UniLoadMoreStatus>('loading')
//订单列表规范
const total = ref<number>(0)
const pageTotal = ref<number>(0)
//订单详情
const orderList = ref<OrderItem[]>([])
const getList = async (isMore = false) => {
  const res = await getPatientConsultOrderListAPI(query.value)
  // loadMoreStatus.value = 'noMore'
  // orderList.value = res.data.rows
  if (isMore) {
    orderList.value.push(...res.data.rows)
  } else {
    orderList.value = res.data.rows
  }
  total.value = res.data.total
  pageTotal.value = res.data.pageTotal
  // console.log('列表信息', orderList.value)
  //判断是否还有更多数据
  if (query.value.current < pageTotal.value) {
    loadMoreStatus.value = 'more'
  } else {
    loadMoreStatus.value = 'noMore'
  }
}

const getMoreData = () => {
  if (query.value.current >= pageTotal.value) return
  query.value.current++
  getList(true)
}
// 是否显示查看更多
const activeShowMoreId = ref('')

getList()

// 下拉刷新
const refresherTriggered = ref(false)
const onRefresh = async () => {
  refresherTriggered.value = true
  query.value.current = 1
  await getList()
  refresherTriggered.value = false
}
//取消订单
const deleteOrder = async (id: string) => {
  const res = await DeletePatientOrderAPI(id)
  console.log('取消订单', res)
  if (res.code === 10000) {
    getList()
  }
}
</script>
<template>
  <scroll-view
    refresher-enabled
    :refresher-triggered="refresherTriggered"
    refresher-background="#f6f6f6"
    class="uni-scroll-view"
    scroll-y
    @scrolltolower="getMoreData"
    @refresherrefresh="onRefresh"
  >
    <view class="consult-list">
      <view class="consult-list-item" v-for="item in orderList" :key="item.id">
        <view class="consult-header">
          <view class="label">
            <image class="doctor-avatar" src="/static/uploads/doctor-avatar.jpg" />
            <text>{{ item.typeValue }}</text>
          </view>
          <text
            class="status"
            :class="{
              'color-1': item.status === OrderStatus.DaiZhiFu,
              'color-2': [OrderStatus.DaiJieZhen, OrderStatus.ZiXunZhong].includes(item.status),
              'color-3': [OrderStatus.YiWanCheng, OrderStatus.YiQuXiao].includes(item.status),
            }"
          >
            {{ item.statusValue }}
          </text>
        </view>

        <navigator
          class="consult-body"
          hover-class="none"
          :url="`/subpkg_consult/pages/order_detail/order_detail?id=${item.id}`"
        >
          <uni-list :border="false">
            <uni-list-item :border="false" title="病情描述" :right-text="item.illnessDesc" />
            <uni-list-item :border="false" title="价格" :right-text="`¥ ${item.payment}`" />
            <uni-list-item :border="false" title="创建时间" :right-text="item.createTime" />
          </uni-list>
        </navigator>

        <view class="consult-footer">
          <view class="uni-button more" v-if="item.status === OrderStatus.YiWanCheng">
            <text class="label" @click="activeShowMoreId = item.id"> 更多 </text>
            <uni-transition :show="activeShowMoreId === item.id">
              <view class="list">
                <view class="list-item">查看处方</view>
                <view class="list-item" @click="deleteOrder(item.id)">删除订单</view>
              </view>
            </uni-transition>
          </view>
          <template v-if="item.status === OrderStatus.DaiZhiFu">
            <button class="uni-button minor">取消订单</button>
            <button class="uni-button">去支付</button>
          </template>

          <template v-if="item.status === OrderStatus.DaiJieZhen">
            <button class="uni-button minor">取消订单</button>
            <button class="uni-button">继续咨询</button>
          </template>

          <template v-if="item.status === OrderStatus.ZiXunZhong">
            <button class="uni-button">继续咨询</button>
          </template>

          <template v-if="item.status === OrderStatus.YiWanCheng">
            <button class="uni-button minor">问诊记录</button>
            <button v-if="item.evaluateId" class="uni-button minor">查看评价</button>
            <button v-else class="uni-button">去评价</button>
          </template>

          <template v-if="item.status === OrderStatus.YiQuXiao">
            <button class="uni-button minor">删除订单</button>
            <button class="uni-button">咨询其它医生</button>
          </template>
        </view>
      </view>

      <!-- 加载状态 -->
      <uni-load-more
        :status="loadMoreStatus"
        color="#C3C3C5"
        :icon-size="16"
        :content-text="{
          contentdown: '上拉显示更多',
          contentrefresh: '数据正在加载中',
          contentnomore: '没有更多数据了',
        }"
      />
    </view>
  </scroll-view>
</template>

<style lang="scss">
@import './scroll-list.scss';
</style>
